/**
 * Contains custom JavaScript code
 */
var urlHolderProject = new Object();

function loadTableProject() {
	toggleFormsProject('hideProject');
	$.get(urlHolderProject.recordsProject, function(response) {
 		$('#tableProjects').find('tbody').remove();
 		
 		for (var i=0; i<response.projects.length; i++) {
			var row = '<tr>';
			row += '<td><input type="radio" name="index" id="index" value="'+i+'"></td>';
			row += '<td>' + response.projects[i].projectId + '</td>';
			row += '<td>' + response.projects[i].projectName + '</td>';
			row += '<td>' + response.projects[i].projectStartDate + '</td>';
			row += '<td>' + response.projects[i].projectEndDate + '</td>';
			row += '</tr>';
	 		$('#tableProjects').append(row);
 		}
  		$('#tableProjects').data('model', response.projects);
		
 	});
}

function submitNewRecordProject() {
	$.post(urlHolderProject.addProject, {
		projectId: $('#newProjectId').val(),
		projectName: $('#newProjectName').val(),
		projectStartDate: $('#newProjectStartDate').val(),
		projectEndDate: $('#newProjectEndDate').val()
		}, 
		function(response) {
			if (!response == null) {
				loadTableProject();
				toggleFormsProject('hideProject'); ;
				toggleCrudButtonsProject('showProject');
				alert('Success! Record has been added.');
			} else {
				alert('Record Already Exists !');
			}
		}
	);	
}


function submitUpdateRecordProject() {
	$.post(urlHolderProject.editProject, {
		projectId: $('#editProjectId').val(),
		projectName: $('#editProjectName').val(),
		projectStartDate: $('#editProjectStartDate').val(),
		projectEndDate: $('#editProjectEndDate').val()
		}, 
		function(response) {
			if (response != null) {
				loadTableProject();
				toggleFormsProject('hideProject'); ;
				toggleCrudButtonsProject('showProject');
				alert('Success! Record has been edited.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

/*function getRole(role) {
	if (role == 1) {
		return 'Admin';
	} else if (role == 2) {
		return 'Regular';
	} else {
		return 'Unknown';
	} 
}*/

function hasSelected() {
	var selected = $('input:radio[name=index]:checked').val();
	if (selected == undefined) { 
		alert('Select a record first!');
		return false;
	}
	
	return true;
}

function fillEditFormProject() {
	var selected = $('input:radio[name=index]:checked').val();
	$('#editProjectId').val( $('#tableProjects').data('model')[selected].projectId );
	$('#editProjectName').val( $('#tableProjects').data('model')[selected].projectName );
	$('#editProjectStartDate').val( $('#tableProjects').data('model')[selected].projectStartDate );
	$('#editProjectEndDate').val( $('#tableProjects').data('model')[selected].projectEndDate );
}

function resetNewFormProject() {
	$('#newProjectId').val('');
	$('#newProjectName').val('');
	$('#newProjectStartDate').val('');
	$('#newProjectEndDate').val('');
}

function resetEditFormProject() {
	$('#editProjectName').val('');
	$('#editProjectStartDate').val('');
	$('#editProjectEndDate').val('');
}

function toggleFormsProject(id) {
	if (id == 'hideProject') {
		$('#newFormProject').hide();
		$('#editFormProject').hide();
		
	} else if (id == 'newProject') {
 		resetNewForm();
 		$('#newFormProject').show();
 		$('#editFormProject').hide();
 		
	} else if (id == 'editProject') {
 		resetEditForm();
 		$('#newFormProject').hide();
 		$('#editFormProject').show();
	}
}

function toggleCrudButtonsProject(id) {
	if (id == 'showProject') {
		$('#newBtnProject').removeAttr('disabled');
		$('#editBtnProject').removeAttr('disabled');
		/*$('#deleteBtnProject').removeAttr('disabled');*/
		$('#reloadBtnProject').removeAttr('disabled');
		
	} else if (id == 'hideProject'){
		$('#newBtnProject').attr('disabled', 'disabled');
		$('#editBtnProject').attr('disabled', 'disabled');
		/*$('#deleteBtnProject').attr('disabled', 'disabled');*/
		$('#reloadBtnProject').attr('disabled', 'disabled');
	}
}
